import csv
import json
import os

# 输入 CSV 文件路径和输出 JSON 文件路径
CSV_PATH = "../fujian/fujian5.csv"
OUTPUT_JSON_PATH = "../fujian/fujian5/origin_data.json"

def csv_to_json(csv_path=CSV_PATH, output_json_path=OUTPUT_JSON_PATH):
    # 确保输出文件夹存在
    os.makedirs(os.path.dirname(output_json_path), exist_ok=True)

    data = []
    
    with open(csv_path, mode='r', encoding='gbk') as csv_file:
        reader = csv.reader(csv_file)
        
        # 跳过第一行表头
        next(reader)
        
        for row in reader:
            # 直接从每一列提取信息
            entry = {
                "category_id": row[0],
                "high_category": row[1],
                "item_type": row[2]
            }
            data.append(entry)

    # 将数据写入 JSON 文件
    with open(output_json_path, mode='w', encoding='utf-8') as json_file:
        json.dump(data, json_file, ensure_ascii=False, indent=4)
    print(f"数据已成功写入到 {output_json_path}")

# 运行函数
csv_to_json()
